<?php
namespace Home\Controller;
use Think\Controller;

class OrderController extends BaseController  {

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index(){
        header("Content-Type:text/html; charset=utf-8");

        $Lock = M('locktable');

        $locklist = $Lock->select();

        foreach ($locklist as $item){
//            echo $item['lockname'];
            $this->assign('x'.$item['id'].'name',$item['lockname']);
            $this->assign('x'.$item['id'].'saleprice',$item['price']);
        }

        $canUseWxPay = canUseWxPay();
        $this->assign('canUseWxPay', $canUseWxPay);


        $this->display();



    }
    // orderdata:lockid-左右规格-内外规格-购买数量-单价
    // 600-left-in-5-299@600-right-out-13-399@610-left-in-2-199
    public function pay(){
        header('Content-Type: application/json; charset=utf-8');

        $Lock = M('locktable');
        $locklist = $Lock->select();

        $res = array("status" => "fail", "msg" => "");

        $res['orderdata'] = $orderdata = $_POST['order'];
        $res['address'] = $address =  $_POST['address'];
        $res['username'] = $username =  $_POST['username'];
        $res['userphone'] = $userphone =  $_POST['userphone'];
        $res['remark'] = $remark =  $_POST['remark'];
        $res['totalprice'] = $totalprice =  $_POST['totalprice'];
        $res['paytype'] = $paytype =  $_POST['paytype'];
        $res['from'] = $from =  $_POST['from'];
        $res['status'] = "success";

        if(is_null($totalprice) || $totalprice == '' || $totalprice == '0' || $totalprice <= 0){

            $res = array("status" => "fail", "msg" => "您的订单有问题,请重新选择商品。");
            echo json_encode($res);
            return;
        }

        $lockname ="";

        $totalprice_temp = 0;
        $temp= explode('@',$orderdata);

        foreach ($temp as $order){
            $arr = explode('-',$order);
            $totalprice_temp = $totalprice_temp+(int)$arr[3]*floatval(getLockprice($locklist,$arr[0]));
            $lockname = $lockname.getLockname($locklist,$arr[0]);
        }

        if($totalprice_temp != $totalprice){
            $res = array("status" => "fail", "msg" => "总金额有误");
            echo json_encode($res);
            return;
        }
        $res['lockname'] = $lockname;

        // 保存订单
        $orderMdl = new \Common\Model\LockorderModel();

        $res['orderid'] = $orderMdl->saveOrder($res);


        if ('alipay' === $paytype) {
            $paymentLogic = new \Common\Logic\PayLogic();
            $payData = array(
                'lockname' => $lockname,
                'pay_id' => $res['orderid'],
                'paytotal' => $totalprice_temp,
                'pay_expire' => 60, // 支付宝超时时间, 分钟
            );


            $form = $paymentLogic->createAlipayForm($payData);

            if ($form) {
                $res['status'] = 'success';
                $res['gateway'] = 'alipay';
                $res['payid'] = $res['orderid'];
                $res['raw'] = $form;
            } else {
                $res['status'] = 'fail';
                $res['gateway'] = 'alipay';
                $res['raw'] = '';
                $res['msg'] = '生成支付请求失败, 请稍候重试';
            }
        }else if('wxpay' === $paytype){
            $paymentLogic = new \Common\Logic\PayLogic();
            $payData = array(
                'lockname' => $lockname,
                'pay_id' => $res['orderid'],
                'paytotal' => $totalprice_temp*100,
                'pay_expire' => date('YmdHis', time() + 3600), // 微信支付的超时时间, YmdHis格式
                'openid' => $_SESSION['openid'],
            );
            $wxPackage = $paymentLogic->createWxpayPackage($payData);
            if ($wxPackage) {
                $res['status'] = 'success';
                $res['gateway'] = 'wxpay';
                $res['raw'] = $wxPackage;
                $res['longip'] = ip2long(get_client_ip());
            } else {
                $res['status'] = 'fail';
                $res['gateway'] = 'wxpay';
                $res['raw'] = '';
                $res['msg'] = '生成支付请求失败, 请稍候重试';
            }
        }



        exit(json_encode($res));
    }






    public function address(){
        $this->display();
    }


    /**
     * 查询订单状态
     */
    public function jumporder()
    {
        $inputOrderid = I('get.orderid', '');
        if (empty($inputOrderid)) {
            E('传入订单不正确!');
        }
        $payMdl = new \Common\Model\LockorderModel();
        $payInfo = $payMdl->getOrderInfo($inputOrderid);

        $this->assign("paystatus", $payInfo['paystatus']);
        $this->assign("orderid", $inputOrderid);
        $this->display();
    }

    public function paysuccess(){
        $this->display();
    }


    public function payfail(){
        $this->display();
    }






}

